package com.ysy.tz.mfg.controller;

import com.ysy.tz.core.annotation.Service;
import com.ysy.tz.core.engine.ParameterInitializable;
import com.ysy.tz.core.selfcomp.CustomerAlert;
import com.ysy.tz.core.selfcomp.CustomerDialogPane;
import com.ysy.tz.core.utils.AESUtil;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.core.validator.Validator;
import com.ysy.tz.mfg.entity.UserEntity;
import com.ysy.tz.mfg.service.UserEditService;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.ButtonType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;

public class UserChangePwdController implements ParameterInitializable {
    @FXML
    private CustomerDialogPane mainPane;
    @FXML
    private PasswordField originalPassword;
    @FXML
    private PasswordField password;
    @FXML
    private PasswordField rePassword;
    UserEntity userEntity;
    @Service
    UserEditService userEditService;
    private Validator validator = new Validator();
    ResourceBundle resourceBundle;

    public UserChangePwdController() {
    }

    public void initialize(URL location, ResourceBundle resources, Object... params) {
        this.userEntity = (UserEntity)params[0];
        this.resourceBundle = resources;
        this.validator.createCheck().dependsOn("originalPassword", this.originalPassword.textProperty()).withMethod((c) -> {
            String pw = (String)c.get("originalPassword");
            if (StringUtils.isBlank(pw)) {
                c.error("originalPassword must not be null!");
            } else if (!pw.equals(AESUtil.Dcodes(this.userEntity.getPassword()))) {
                c.error("originalPassword wrong!");
            }

        }).decorates(this.originalPassword).immediateNoRecheck();
        this.validator.createCheck().dependsOn("password", this.password.textProperty()).withMethod((c) -> {
            String pw = (String)c.get("password");
            if (StringUtils.isBlank(pw)) {
                c.error("password must not be null!");
            }

        }).decorates(this.password).immediateNoRecheck();
        this.validator.createCheck().dependsOn("rePassword", this.rePassword.textProperty()).withMethod((c) -> {
            String rpw = (String)c.get("rePassword");
            if (StringUtils.isBlank(rpw)) {
                c.error("rePassword must not be null!");
            }

        }).decorates(this.rePassword).immediateNoRecheck();
        this.validator.createCheck().dependsOn("password", this.password.textProperty()).dependsOn("rePassword", this.rePassword.textProperty()).withMethod((c) -> {
            String pw = (String)c.get("password");
            String rpw = (String)c.get("rePassword");
            if (!pw.equals(rpw)) {
                c.error("password not equals rePassword!");
            }

        }).decorates(this.password).decorates(this.rePassword).immediateNoRecheck();
    }

    public void unload() {
    }

    public void submit() {
        if (this.validator.validate()) {
            UserEntity targetUser = new UserEntity();
            targetUser.setUserId(this.userEntity.getUserId());
            targetUser.setPassword(AESUtil.Ecodes(this.password.getText()));
            if (this.userEditService.editUser(targetUser)) {
                UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
                if (currentUser.getUserId().equals(targetUser.getUserId())) {
                    currentUser.setPassword(targetUser.getPassword());
                }

                this.hideDialog();
            } else {
                (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("alert_userEdit_warn"), new ButtonType[0])).showAndWait();
            }
        }

    }

    public void hideDialog() {
        Stage stage = (Stage)this.mainPane.getScene().getWindow();
        stage.close();
    }
}
